﻿using Infrastructure;
using Mapster;
using ZR.Model;
using ZR.Model.Souvenir;
using ZR.Model.Souvenir.Dto;
using ZR.Repository;
using ZR.Service.Souvenir.IService;

namespace ZR.Service.Souvenir
{
    /// <summary>
    /// 收货地址Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IShippingAddressService), ServiceLifetime = LifeTime.Transient)]
    public class ShippingAddressService : BaseService<ShippingAddress>, IShippingAddressService
    {
        
        /// <summary>
        /// 查询收货地址列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<ShippingAddressDto> GetList(ShippingAddressQueryDto parm)
        {
            int total = 0;
            var predicate = Expressionable.Create<ShippingAddress>();
            predicate.And(sa => sa.Userid == parm.UserId);
            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPageList(parm.PageNum, parm.PageSize, ref total);
            var config = new TypeAdapterConfig();
            config.ForType<ShippingAddress, ShippingAddressDto>()
                .Map(dest => dest.Pca, src => 
                    new[] { src.Province, src.City, src.Area });
            var page = new PagedInfo<ShippingAddressDto>()
            {
                PageSize = parm.PageSize,
                PageIndex = parm.PageNum,
                Result = response.Adapt<List<ShippingAddressDto>>(config),
                TotalNum = total
            };
            return page;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ShippingAddress GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加收货地址
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ShippingAddress AddShippingAddress(ShippingAddressDto model)
        {
            var isExistDefaultAddress = Queryable()
                .Where(it => it.Userid == model.Userid && it.Isdefault == "Y")
                .Count() > 0;
            if (isExistDefaultAddress) throw new CustomException("只能添加一个默认地址");
            var config = new TypeAdapterConfig();
            config.ForType<ShippingAddressDto, ShippingAddress>()
                .Map(dest => dest.Province, src => src.Pca[0])
                .Map(dest => dest.City, src => src.Pca[1])
                .Map(dest => dest.Area, src => src.Pca[2]);
            return Context.Insertable(model.Adapt<ShippingAddress>(config)).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改收货地址
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateShippingAddress(ShippingAddressDto model)
        {
            var isExistDefaultAddress = Queryable()
                .Where(it => it.Userid == model.Userid && it.Isdefault == "Y")
                .Count() > 0;
            if (isExistDefaultAddress) throw new CustomException("只能添加一个默认地址");
            var config = new TypeAdapterConfig();
            config.ForType<ShippingAddressDto, ShippingAddress>()
                .Map(dest => dest.Province, src => src.Pca[0])
                .Map(dest => dest.City, src => src.Pca[1])
                .Map(dest => dest.Area, src => src.Pca[2]);
            return Updateable(model.Adapt<ShippingAddress>(config))
                .ExecuteCommand();
        }
    }
}